package com.liu.frame;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.liu.frame.activity.MeActivity;
import com.liu.frame.activity.TestWebPActivity;
import com.liu.frame.base.BaseActivity;
import com.liu.frame.beans.HomeBean;
import com.liu.frame.beans.ProductBean;
import com.liu.frame.beans.UserBean;
import com.liu.frame.config.Api;
import com.liu.frame.fragment.HomeFragment;
import com.liu.frame.http.WeiLiCaiNetWorks;
import com.liu.frame.utils.EventBusUtils;
import com.liu.frame.utils.LogUtils;
import com.liu.frame.utils.SharedPreferencesUtils;
import com.liu.frame.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.util.List;


public class MainActivity extends BaseActivity {
    private TelephonyManager telephonyManager;
    public UserBean userBean;
    private List<ProductBean> productBeanList;
    private Button btn_login, btn_home, btn_remove;
    private TextView tv_content;


    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_main);
        telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    }

    @Override
    public void setTitleView() {
        myTitleBar=$(R.id.my_titlebar);
        myTitleBar.setVisibility(View.VISIBLE);
        myTitleBar.setTitle("首页");
        myTitleBar.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void initView() {
        btn_home = $(R.id.btn_home);
        btn_login = $(R.id.btn_login);
        tv_content = $(R.id.tv_content);
        btn_remove = $(R.id.btn_remove);
        EventBusUtils.register(mActThis);

    }

    @Override
    public void initEvents() {
        btn_home.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        btn_remove.setOnClickListener(this);

    }

    @Override
    public void objectLogic() {
//        getProduct();
        addFragment(new HomeFragment(), R.id.fragment_home);
//        FragmentManager fragmentManager = this.getFragmentManager();
//        FragmentTransaction transaction = fragmentManager.beginTransaction();
//        transaction.add(R.id.fragment_home, new HomeFragment());
//        transaction.commit();


    }

    @Override
    public void onClickEvent(View v) {
        switch (v.getId()) {

            case R.id.btn_home:
//                getProduct();
                getHomeData();

                break;
            case R.id.btn_login:
                login();

                break;
            case R.id.btn_remove:

                break;
            default:
        }

    }

    @Override
    public void loadCache() {
        try {
            productBeanList = new SharedPreferencesUtils(mActThis).getList(Api.PRODUCT);
            if (productBeanList != null && productBeanList.size() > 0) {

                for (ProductBean p : productBeanList
                        ) {
                    LogUtils.e(p.getName());

                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 登陆
     */
    private void login() {
        parametersMap.put("telephone", "18502830801");
        parametersMap.put("password", "a123456");
        parametersMap.put("devicekey", telephonyManager.getDeviceId());
        WeiLiCaiNetWorks<UserBean> netWorks = new WeiLiCaiNetWorks<UserBean>(mActThis) {
        };
        netWorks.post(service.login(parameters.postAllParameters(parametersMap)), new WeiLiCaiNetWorks.CallBack<UserBean>() {
            @Override
            public void start() {
                LogUtils.e("start");

            }

            @Override
            public void completed() {
                LogUtils.e("completed");

            }

            @Override
            public void error() {
                LogUtils.e("error");
            }

            @Override
            public void fail(String code, String message, String returnInfo) {

                LogUtils.e("fail");
            }

            @Override
            public void success(String code, String message, UserBean userBean) {
                LogUtils.e("success");
                LogUtils.e(userBean.toString());
                EventBusUtils.postSticky(userBean);
                ToastUtils.myToast(mActThis, "登陆成功");
                new SharedPreferencesUtils(mActThis).putObject(Api.LOGIN, userBean);
                startActivity(new Intent(MainActivity.this, MeActivity.class));
            }
        });

    }

    public void getHomeData() {
        WeiLiCaiNetWorks<HomeBean> homeBeanWeiLiCaiNetWorks = new WeiLiCaiNetWorks<HomeBean>(mActThis) {
        };
        subscription = homeBeanWeiLiCaiNetWorks.post(service.getHomeBean(parameters.postAllParameters(parametersMap)), new WeiLiCaiNetWorks.CallBack() {
            @Override
            public void start() {

            }

            @Override
            public void completed() {

            }

            @Override
            public void error() {

            }

            @Override
            public void fail(String code, String message, String returnInfo) {

            }

            @Override
            public void success(String code, String message, Object o) {
                startActivity(new Intent(mActThis, TestWebPActivity.class));

            }
        });
    }

    public void getProduct() {
        WeiLiCaiNetWorks<ProductBean> netWorks = new WeiLiCaiNetWorks<ProductBean>(mActThis) {
        };


        subscription = netWorks.post(service.getProduct(parameters.postAllParameters(parametersMap)),
                new WeiLiCaiNetWorks.CallBack<List<ProductBean>>() {
                    @Override
                    public void start() {

                    }

                    @Override
                    public void completed() {

                    }

                    @Override
                    public void error() {

                    }

                    @Override
                    public void fail(String code, String message, String returnInfo) {

                    }

                    @Override
                    public void success(String code, String message, List<ProductBean> productBeanList) {
//                LogUtils.e(productBeanList.get(0).toString());
                        EventBusUtils.post(productBeanList.get(0).getName());
                        try {
                            new SharedPreferencesUtils(mActThis).putList(Api.PRODUCT, productBeanList);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        for (ProductBean p : productBeanList
                                ) {
                            LogUtils.e(p.getName());

                        }

                    }
                });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(mActThis);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginSuccess(UserBean user) {
        tv_content.setText(user.toString());

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void product(ProductBean product) {
        ToastUtils.myToast(mActThis, product.getName());

    }
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public  void getString(String s){
//        LogUtils.e("1111"+s);
//    }
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public  void getString1(String s){
//        LogUtils.e("2222"+s);
//    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK){
            if (getFragmentManager().getBackStackEntryCount()==1){
                finish();
                return true;

            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
